/**
 * 插件配置中心
 */
const path = require('path')
const __define__ = require('../../../defineConfig')
const packageJson = require(path.join(__dirname, "../../../package.json"))
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const Webpack = require('webpack'); //引入webpack模块
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const WebpackObfuscator = require('webpack-obfuscator');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const WebpackBarPlugin = require('webpackbar');

/**
 * webpack插件将打包好的文件注入到html模板里
 */
const htmlWebpackPlugin = new HtmlWebpackPlugin({
  template: path.join(__dirname, "../../../public/index.html"),
  filename: "./index.html"
});


const definePlugin = new Webpack.DefinePlugin({
  PRODUCTION: JSON.stringify(true),
  VERSION: JSON.stringify('5fa3b9'),
  BROWSER_SUPPORTS_HTML5: true,
  TWO: '1+1',
  'typeof window': JSON.stringify('object'),
  'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
});

/** 控制台信息设置*/
const friendlyPlugin = new FriendlyErrorsWebpackPlugin({
  compilationSuccessInfo: {
    // messages: [`You application is running here http://localhost:${devServer.port}`],
    notes: ['successful compilation'],
  },
  clearConsole: true,
})


/**控制台显示编译进度 */
const progressBarPlugin = new WebpackBarPlugin({
  color: "#85d", // 默认green，进度条颜色支持HEX
  basic: true, // 默认true，启用一个简单的日志报告器
  profile: false, // 默认false，启用探查器。
})


/*** 代码混淆*/
const javaScriptObfuscator = new WebpackObfuscator({
  rotateStringArray: true,
}, [])



class resolvePlugins {

  constructor(options) {
    this.defaultPlugins = [
      htmlWebpackPlugin,
      definePlugin,
      // friendlyPlugin,
      progressBarPlugin,
      new ForkTsCheckerWebpackPlugin(), // 使用babel编译ts语法，该插件配合做类型检测
      // new ESBuildPlugin,
      // new Webpack.HotModuleReplacementPlugin(),
      new Webpack.AutomaticPrefetchPlugin(), // 改进增量构建，不一定有作用
    ]
    this.bundleAnalyzerConf = {
      analyzerMode: 'server',
      analyzerPort: 8888,
      openAnalyzer: true,
      generateStatsFile: false,
      statsFilename: 'stats.json',
      logLevel: 'info',
      defaultSizes: 'parsed',
    }
    this.provideConf = {
      'React': 'react'
    }
    this.options = options
  }


  generatePlugins = () => {
    switch (__define__) {
      case __define__.jsObfuscator:
        this.defaultPlugins.push(javaScriptObfuscator)

      case __define__.analyze:
        this.bundleAnalyzerConf = {
          ...this.bundleAnalyzerConf,
          ...__define__.analyze
        }

      default:
        null
    }

    if (process.env.NODE_ENV === 'product') {
      const bundleAnalyzerPlugin = new BundleAnalyzerPlugin(this.bundleAnalyzerConf)
      this.defaultPlugins.push(bundleAnalyzerPlugin)
    }

    /**提供变量插件，可以免去每个页面引入React */
    if (__define__.hasOwnProperty('provide') && !__define__.provide) {
      return
    } else {
      if (typeof __define__.provide === 'objct') {
        this.provideConf = {
          ...this.provideConf,
          ...__define__.provide
        }
      }
      const providePlugin = new Webpack.ProvidePlugin(this.provideConf)
      this.defaultPlugins.push(providePlugin)
    }

    return this.defaultPlugins
  }
}



const pluginsClass = new resolvePlugins()

const plugins = pluginsClass.generatePlugins()

module.exports = plugins